import {View, ScrollView, Text, Image} from '@tarojs/components';
import Taro, {getCurrentInstance} from '@tarojs/taro';
import React, {Component} from 'react';
import {connect} from 'react-redux';
import './index.less';
import * as T from './types';
import actions from './actions';
import {store2Props} from './selectors';
import OrderStatus from './components/order-status';
import OrderBuyerInfo from './components/order-buyer-info';
import OrderBody from './components/order-body';
import OrderBottom from './components/order-bottom';
import tip from '../order-confirm/img/presale.png';
import {msg} from 'wmkit';
import WMLoading from '@/pages/common/loading';
import {getHashParam} from '@/utils/common-functions';
//@ts-ignore
actions().actions.loadReducer();
@connect<Partial<T.IProps>, any>(store2Props, actions)
export default class OrderDetail extends Component<Partial<T.IProps>, any> {
  constructor(props) {
    super(props);
    msg.on({
      'payment-leave': () => {
        this._initFun();
      },
    });
  }

  componentWillMount(): void {
    if (__TARO_ENV !== 'h5') {
      Taro.showShareMenu({
        withShareTicket: true,
      });
    }
    Taro.removeStorageSync('mini::returnSuccessBackFlag');
  }

  componentDidMount() {
    this._initFun();
  }

  componentDidShow() {
    this._initFun();
  }

  onShareAppMessage() {
    let wechatShareInfo = JSON.parse(Taro.getStorageSync('wechatShareInfo'));
    return {
      title: wechatShareInfo.title,
      imageUrl: wechatShareInfo.imgUrl[0].url,
    };
  }

  async componentWillUnmount() {
    this.props.actions.clean();
    getCurrentInstance().router;
    //清空缓存
    await this.props.actions.action._cleanLoacl();
  }

  render() {
    let {main} = this.props;
    return (
      <View className="packageCOrderDetail">
        {main?.isThirdPlatform && (
          <View className="tip-info">
            <Image src={tip} className="tip-info-image"></Image>
            <Text className="tip-info-text">订单包含需要向供应商采购的商品，如采购失败会进行自动退款</Text>
          </View>
        )}
        <View className="orderDetailScroll">
          {/*订单状态*/}
          <OrderStatus />

          {/*收货人信息*/}
          <OrderBuyerInfo />

          {/*订单主体*/}
          <OrderBody />
        </View>
        {/*操作按钮,推广订单详情不展示操作按钮*/}
        {!main?.promotionOrder && <OrderBottom />}
        {main?.isLoadingFlag && <WMLoading />}
      </View>
    );
  }

  _initFun = async () => {
    let {main} = this.props;
    let tid = getCurrentInstance().router.params
      ? getCurrentInstance().router.params.id
      : main?.param
        ? main?.param.id
        : '';
    let pointsOrder = getCurrentInstance().router.params
      ? getCurrentInstance().router.params.type == 'pointsOrder'
      : main?.param.type == 'pointsOrder'; //积分商城的兑换列表的订单进入
    let promotionOrder = getCurrentInstance().router.params
      ? getCurrentInstance().router.params.type == 'promotionOrder'
      : main?.param.type == 'promotionOrder'; //推广订单进入
    if (__TARO_ENV === 'h5' && getCurrentInstance().router.params) {
      this.props.actions.action.commonChange('main.param', getCurrentInstance().router.params);
    }
    let usePoint = main?.points?.usePoint;
    await this.props.actions.init(tid, pointsOrder, promotionOrder, usePoint);

    await this.props.actions.action.saveTid(tid);
    //缓存处理
    await this.props.actions.action._getLoacl();
    //积分初始化
    await this.props.actions.action._pointInit();
  };
}
